// 配置分页插件的目录
layui.extend({
    customPage: '../lib/customPage/custom-page',
    apiconfig: '../public/apiconfig'
});
layui.use(['form',"table","laypage","customPage","laydate"], function () {
    var form = layui.form,
        table = layui.table,
        laypage = layui.laypage,
        laydate = layui.laydate,
        customPage = layui.customPage //自定义分页插件
        element = layui.element;
        var mainTop=$(".main_top").outerHeight()
        $(".tableMb").height(window.screen.height-(230+mainTop+10));
        // 数据字典获取值--------------
        userDept(form,'userDept');
          // 数据字典获取值--------------
        $.ajaxSetup({ cache: false })
        var searchstr={},loading;
        customPageFun(searchstr)
        table.render({
            elem: '#tabList'
            ,title: '用户表'
            ,toolbar: '<div><a class="layui-btn layui-btn-radius bao_loginbtnLO" lay-event="exposrt"  id="download">导出</a><a class="layui-btn layui-btn-radius bao_loginbtnLO" lay-event="addUser" id="addUser">新增</a></div>'
            ,defaultToolbar: []
            // ,page: true //开启分页
            ,cols: [
                [{ type: 'checkbox', title: '选择',width: 30},
                { type: 'numbers', title: '序号', width: 40},
                {field: 'userDept',title: '处室',align:"center"}, 
                {field: 'loginName', title: '账号',align:"center" }, 
                {field: 'ER', title: '操作',align:"center", width: 120,templet:function(){
                   return '<a class="layui-btn layui-btn-xs" data-title="重置密码" lay-event="newworkpass">重置密码</a>'
                }}
                ]
            ],
            done: function(data) {
            }
        }); 

   //执行示例 如何采用 table.reload 方法分页的渲染放到 table之后
   function customPageFun(searchstr){
    customPage.render({
        elem: '#page',
        layout: ['skipFirst', 'prev', 'skip', 'next', 'skipLast', 'count'],
        url: 'gt/opm/user/page',
        method: 'GET',
        request: {
            pageName: 'pageNum',
            limitName: 'pageSize'
        },
        where: {
            userDept:searchstr.userDept,
            loginName:searchstr.loginName,
        },
        parseData:function(res){
            return {
                "code":res.code, //解析接口状态
                "flag":res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "data": res.data.rows, //解析数据列表
                "total":res.data.total
            };
        },
        done: function(res, curr, data) {
            if(res.flag==true){
                table.reload('tabList', {
                    data: res.data,
                    limit: res.size
                });
            }else{
                layer.alert(res.msg)
            }
            
        }
    });
}
    // 监听列表点击事件
    table.on('tool(tabList)', function(obj){
       if(obj.event=="newworkpass"){
        $.ajax({
            url: "gt/opm/user/reset/"+obj.data.id, //请求地址
            type: "put", //提交的方式S
            dataType: "JSON", //返回类型 TEXT字符串 JSON XML
            beforeSend: function() {
                loading=layer.load(2);
            },
            ContentType:"application/json;charset=UTF-8",
            success: function(data) { 
                layer.closeAll('loading');
                if (data.flag == true) {
                    layer.closeAll();
                    layer.msg(data.msg,{icon:1,time:2000});
                    customPageFun(searchstr);
                }else{
                    layer.msg(data.msg,{icon:2,time:2000});
                }
            }
        })
       }
    })
    table.on('toolbar(tabList)', function(obj){
        
        if(obj.event=="addUser"){
            layer.open({
                type: 1
                ,title:"新增"
                ,shade:[0.5, '#000']
                ,id: 'layerDemo1' //防止重复弹出
                ,area:['345px','320px']
                ,content: html
                ,btn: ["确定","取消"]
                ,btnAlign: 'c' //按钮居中
                ,yes: function(){
                    form.on('submit(Editpassword)',function(d){
                        var str={
                            "loginName":d.field.loginName,
                            "userDept":d.field.userDept
                        }
                        $.ajax({
                            url: "gt/opm/user", //请求地址
                            type: "POST", //提交的方式S
                            data: str, //提交的数据
                            dataType: "JSON", //返回类型 TEXT字符串 JSON XML
                            beforeSend: function() {
                                loading=layer.load(2);
                            },
                            ContentType:"application/json;charset=UTF-8",
                            success: function(data) { 
                                layer.closeAll('loading');
                                if (data.flag == true) {
                                    layer.closeAll();
                                    layer.msg(data.msg,{icon:1,time:2000});
                                    customPageFun(searchstr);
                                }else{
                                    layer.msg(data.msg,{icon:2,time:2000});
                                }
                            }
                        })
                        
                    })
                }
                ,success: function(layero) {
                    // 添加form标识
                    layero.addClass('layui-form');
                    // 将保存按钮改变成提交按钮
                    layero.find('.layui-layer-btn0').attr({
                        'lay-filter': 'Editpassword',
                        'lay-submit': ''
                });}
              }); 
        }
        if(obj.event=="exposrt"){
            if (table.exportCols.length > 0 && table.cache.tabList.length > 0) {
            // if(checkStatus.data!=''){
                var loginName=$("#loginName").val(),
                userDept=$("#userDept option:selected").val();
              
                var url = "gt/opm/user/export?loginName=" + loginName +
                    "&userDept=" + userDept;
                $("#download").attr("href", url);
            } else {
                layer.msg("暂无可导出数据");
            }
        }
     })
    var active={
        search:function(){
            searchstr.loginName=$("#loginName").val();
            searchstr.userDept=$("#userDept option:selected").val();
            customPageFun(searchstr)
        }
    }
   $(".bao_loginbtn").on('click',function(data){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
   })
   var html='<form  class="layui-form" action="" lay-filter="Editpassword">'
        +'<div style="padding:30px 30px 0" class="bao_alert">'
        +'<lable>处室</lable>'
        +'<div class="layui-input-block" style="margin:14px 0;">'
        +'<input type="text" maxlength="90" name="userDept" lay-verify="required" placeholder="请输入处室" autocomplete="off" class="layui-input">'
        +'</div>'
        +'<lable>账号</lable>'
        +'<div class="layui-input-block" style="margin:14px 0;">'
        +'<input type="text" maxlength="90" name="loginName" lay-verify="required" placeholder="请输入账号" autocomplete="off" class="layui-input">'
        +'</div>'
        +'<p class="oneBorder"></p>'
        +'</div>'  
        +'</form>'  
})